clear
set more off
capture log close
clear matrix

log using ${us_results}/iv_parent_incomes_v2.log, replace

******************************************
/* USE 5TH INCOME AS ENDOGENOUS INCOME */
  
*USE the FULL SAMPLE 
use *newid *LAB* AGE *cohort *LABYR* *inc* *rel *MF* *empl* *schmax src year female using ${projdata}/analysis-sample.dta, clear

*** Get parent incomes at five child ages: 12(13) 14(15) 16(17) 18(19) 20(21) 
//foreach age in 12 14 16 18 20 {
foreach age in 16 {
local age2=`age'+1
gen m_temp_`age'=.
replace m_temp_`age'=m_rincwf if AGE==`age'  & inlist(m_rel,2,20,22)
replace m_temp_`age'=m_rinchd if AGE==`age'  & inlist(m_rel,1,10)
replace m_temp_`age'=m_rincwf if AGE==`age2' & inlist(m_rel,2,20,22) & m_temp_`age'==.
replace m_temp_`age'=m_rinchd if AGE==`age2' & inlist(m_rel,1,10) 	 & m_temp_`age'==.
bysort newid m_newid: egen m_LAB_a`age'=mean(m_temp_`age')		// fill this in for all child obs

gen m_tempage_`age'=.
replace m_tempage_`age'=year-m_cohort if AGE==`age'  & m_rincwf!=. & inlist(m_rel,2,20,22)
replace m_tempage_`age'=year-m_cohort if AGE==`age'  & m_rinchd!=. & inlist(m_rel,1,10)
replace m_tempage_`age'=year-m_cohort if AGE==`age2' & m_rincwf!=. & inlist(m_rel,2,20,22) & m_tempage_`age'==.
replace m_tempage_`age'=year-m_cohort if AGE==`age2' & m_rinchd!=. & inlist(m_rel,1,10)    & m_tempage_`age'==.
bysort newid m_newid: egen m_LABAGE_a`age'=mean(m_tempage_`age')	// fill this in for all child obs

gen f_temp_`age'=.
replace f_temp_`age'=f_rincwf if AGE==`age'  & inlist(f_rel,2,20,22)
replace f_temp_`age'=f_rinchd if AGE==`age'  & inlist(f_rel,1,10)
replace f_temp_`age'=f_rincwf if AGE==`age2' & inlist(f_rel,2,20,22) & f_temp_`age'==.
replace f_temp_`age'=f_rinchd if AGE==`age2' & inlist(f_rel,1,10) 	 & f_temp_`age'==.
bysort newid f_newid: egen f_LAB_a`age'=mean(f_temp_`age')		// fill this in for all child obs

gen f_tempage_`age'=.
replace f_tempage_`age'=year-f_cohort if AGE==`age'  & f_rincwf!=.& inlist(f_rel,2,20,22)
replace f_tempage_`age'=year-f_cohort if AGE==`age'  & f_rinchd!=.& inlist(f_rel,1,10)
replace f_tempage_`age'=year-f_cohort if AGE==`age2' & f_rincwf!=.& inlist(f_rel,2,20,22) & f_tempage_`age'==.
replace f_tempage_`age'=year-f_cohort if AGE==`age2' & f_rinchd!=.& inlist(f_rel,1,10) 	  & f_tempage_`age'==.
bysort newid f_newid: egen f_LABAGE_a`age'=mean(f_tempage_`age')	// fill this in for all child obs

}
drop ?_temp*


/*
Be more flexible now. Just get one income before child age 16 and one income after child age 16 (17).
 - "befre": Start at age 12; if missing, then age 13, ..., then age 14, ..., then age 15
 - "after": Start at age 21; if missing, then age 20, ..., then age 19, ..., then age 18
*/

*** Get parent age as far before and after child age 16(17) as possible (in 12-21 range)
gen m_temp_12=.
gen m_tempage_12=.
foreach age in 12 13 14 15 {
replace m_temp_12=m_rincwf if m_temp_12==. & AGE==`age'  & inlist(m_rel,2,20,22)
replace m_temp_12=m_rinchd if m_temp_12==. & AGE==`age'  & inlist(m_rel,1,10)
replace m_tempage_12=year-m_cohort if m_tempage_12==. & m_rincwf!=. & AGE==`age'  & inlist(m_rel,2,20,22)
replace m_tempage_12=year-m_cohort if m_tempage_12==. & m_rinchd!=. & AGE==`age'  & inlist(m_rel,1,10)
}
bysort newid m_newid: egen m_LAB_12=mean(m_temp_12)	// fill this in for all child obs
bysort newid m_newid: egen m_LABAGE_12=mean(m_tempage_12)	// fill this in for all child obs

gen f_temp_12=.
gen f_tempage_12=.
foreach age in 12 13 14 15 {
replace f_temp_12=f_rincwf if f_temp_12==. & AGE==`age'  & inlist(f_rel,2,20,22)
replace f_temp_12=f_rinchd if f_temp_12==. & AGE==`age'  & inlist(f_rel,1,10)
replace f_tempage_12=year-f_cohort if f_tempage_12==. & f_rincwf!=. & AGE==`age'  & inlist(f_rel,2,20,22)
replace f_tempage_12=year-f_cohort if f_tempage_12==. & f_rinchd!=. & AGE==`age'  & inlist(f_rel,1,10)
}
bysort newid f_newid: egen f_LAB_12=mean(f_temp_12)	// fill this in for all child obs
bysort newid f_newid: egen f_LABAGE_12=mean(f_tempage_12)	// fill this in for all child obs

gen m_temp_21=.
gen m_tempage_21=.
foreach age in 21 20 19 18 {
replace m_temp_21=m_rincwf if m_temp_21==. & AGE==`age'  & inlist(m_rel,2,20,22)
replace m_temp_21=m_rinchd if m_temp_21==. & AGE==`age'  & inlist(m_rel,1,10)
replace m_tempage_21=year-m_cohort if m_tempage_21==. & m_rincwf!=. & AGE==`age'  & inlist(m_rel,2,20,22)
replace m_tempage_21=year-m_cohort if m_tempage_21==. & m_rinchd!=. & AGE==`age'  & inlist(m_rel,1,10)
}
bysort newid m_newid: egen m_LAB_21=mean(m_temp_21)	// fill this in for all child obs
bysort newid m_newid: egen m_LABAGE_21=mean(m_tempage_21)	// fill this in for all child obs

gen f_temp_21=.
gen f_tempage_21=.
foreach age in 21 20 19 18 {
replace f_temp_21=f_rincwf if f_temp_21==. & AGE==`age'  & inlist(f_rel,2,20,22)
replace f_temp_21=f_rinchd if f_temp_21==. & AGE==`age'  & inlist(f_rel,1,10)
replace f_tempage_21=year-f_cohort if f_tempage_21==. & f_rincwf!=. & AGE==`age'  & inlist(f_rel,2,20,22)
replace f_tempage_21=year-f_cohort if f_tempage_21==. & f_rinchd!=. & AGE==`age'  & inlist(f_rel,1,10)
}
bysort newid f_newid: egen f_LAB_21=mean(f_temp_21)	// fill this in for all child obs
bysort newid f_newid: egen f_LABAGE_21=mean(f_tempage_21)	// fill this in for all child obs

 
 
* IMPOSE SAMPLE RESTRICTIONS, COMPUTE RANKS, AND SAVE MAIN ANALYSIS SAMPLE
keep if src==1 // SRC sample only
keep if inrange(AGE,25,48)
keep if inrange(cohort,1952,1993)
drop if (m_newid==. & f_newid==.) 	// matched to at least one parent
drop if m_LAB==. & f_LAB==.			// observe at least one parent's income
drop if LAB==.
keep if year>=1977


*Ranks by child birth cohort and child gender (main analyses)
local vars "LAB f_LAB m_LAB "
foreach x of local vars {
	bysort cohort female: egen n=count(`x') 
	bysort cohort female: egen i=rank(`x')  
	bysort cohort female: gen p`x'=(i-1)/(n-1)*100 
	drop n i
}
*Ranks by child birth cohort and child gender (annual incomes for IV approach)
foreach a in a16 12 21 {
local vars`i' " f_LAB_`a' m_LAB_`a' "
foreach x of local vars`i' {
	bysort cohort female: egen n=count(`x') 
	bysort cohort female: egen i=rank(`x')  
	bysort cohort female: gen p`x'=(i-1)/(n-1)*100 
	drop n i
}
}

 
*Age controls for main analyses
gen AGEC1=year-cohort-40 
gen f_LABAGE_1=f_LABYR-f_cohort 
gen m_LABAGE_1=m_LABYR-m_cohort 

forval i=2/4 {
	gen AGEC`i'=AGEC1^`i'
	gen f_LABAGE_`i'=f_LABAGE_1^`i'
	gen m_LABAGE_`i'=m_LABAGE_1^`i'
}

*Age of parent at child age 16 observed income (endogenous income for IV approach)
gen f_LABAGE16_1=f_LABYR_1-f_cohort
gen m_LABAGE16_1=m_LABYR_1-m_cohort

forval i=2/4 {
	gen f_LABAGE16_`i'=f_LABAGE16_1^`i'
	gen m_LABAGE16_`i'=m_LABAGE16_1^`i'
}

*Keep main analysis sample 
keep if year>=1985 
 
* MOTHER's SAMPLE
 keep if pm_LAB!=. & m_LABAGE_1!=.
 keep if m_schmax!=. 
 keep if m_emplavg!=. 
 
* FATHER's SAMPLE
 keep if pf_LAB!=. & f_LABAGE_1!=.
 keep if f_schmax!=. 
 keep if f_emplavg!=. 


*Variable for Early/late time periods
gen period=.
replace period=1 if inrange(year,1985,1995)
replace period=2 if inrange(year,2008,2018)


*Check age at each parental income measure
tabstat m_LABAGE16_1 , by(period) stat(n mean sd min max)
tabstat f_LABAGE16_1 , by(period) stat(n mean sd min max)

tempfile rankedsample
	save `rankedsample'
	
*************************************************
* SPECIFICATION 1: ONE PARENT, ONLY INCOME  
*************************************************
use `rankedsample', clear

*************
* MOTHERS 

  
* RESIDUALIZE average measure for child and mother for IRP with average parent income
 foreach var in pLAB pm_LAB  {
	qui regress `var' m_LABAGE_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE annual measures for mother -- use endogenous measure age
 foreach var in pm_LAB_12  pm_LAB_a16  pm_LAB_21 {
	qui regress `var' m_LABAGE16_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE child income for IV approach (add "1" to variable name) with mothers
 foreach var in pLAB {
	qui regress `var' m_LABAGE16_? AGEC? i.year 
	predict `var'1_r, resid
 }

**** Early period   
preserve
local t=1
 loc yrmin=1985
 loc yrmax=1995
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'm N`t'm

*IRP with parent average income
 regress pLAB_r pm_LAB_r, cluster(newid)
 matrix B`t'm=_b[pm_LAB_r]
 matrix N`t'm=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pm_LAB_a16_r = pm_LAB_`a'_r ), cluster(newid)
 matrix B`t'm=B`t'm,_b[pm_LAB_a16_r]
 matrix N`t'm=N`t'm,e(N)
}
restore 

**** Late period 
preserve
local t=2
 loc yrmin=2008
 loc yrmax=2018
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'm N`t'm

*IRP with parent average income
 regress pLAB_r pm_LAB_r, cluster(newid)
 matrix B`t'm=_b[pm_LAB_r]
 matrix N`t'm=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pm_LAB_a16_r = pm_LAB_`a'_r ), cluster(newid)
 matrix B`t'm=B`t'm,_b[pm_LAB_a16_r]
 matrix N`t'm=N`t'm,e(N)
}
restore  


**************
* FATHERS 

  
* RESIDUALIZE average measure for child and father for IRP with average parent income
 drop pLAB_r
 foreach var in pLAB pf_LAB  {
	qui regress `var' f_LABAGE_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE annual measures for father -- use endogenous measure age
 foreach var in pf_LAB_12  pf_LAB_a16  pf_LAB_21 {
	qui regress `var' f_LABAGE16_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE child income for IV approach (add "1" to variable name) with fathers
 drop pLAB1_r
 foreach var in pLAB {
	qui regress `var' f_LABAGE16_? AGEC? i.year 
	predict `var'1_r, resid
 }

**** Early period   
preserve
local t=1
 loc yrmin=1985
 loc yrmax=1995
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'f N`t'f

*IRP with parent average income
 regress pLAB_r pf_LAB_r, cluster(newid)
 matrix B`t'f=_b[pf_LAB_r]
 matrix N`t'f=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pf_LAB_a16_r = pf_LAB_`a'_r ), cluster(newid)
 matrix B`t'f=B`t'f,_b[pf_LAB_a16_r]
 matrix N`t'f=N`t'f,e(N)
}
restore 

**** Late period 
preserve
local t=2
 loc yrmin=2008
 loc yrmax=2018
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'f N`t'f

*IRP with parent average income
 regress pLAB_r pf_LAB_r, cluster(newid)
 matrix B`t'f=_b[pf_LAB_r]
 matrix N`t'f=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pf_LAB_a16_r = pf_LAB_`a'_r ), cluster(newid)
 matrix B`t'f=B`t'f,_b[pf_LAB_a16_r]
 matrix N`t'f=N`t'f,e(N)
}
restore  


**** Save results to datset 

clear 
set obs 1
gen temp=1 
forv t=1/2 {
 svmat B`t'm
 svmat B`t'f
 svmat N`t'm
 svmat N`t'f
 }
 
 reshape long B1m B2m N1m N2m B1f B2f N1f N2f, i(temp) j(agez) 
 replace agez=0  if agez==1
 replace agez=12 if agez==2
 replace agez=21 if agez==3
 
 label define agez_vals 0 "avg" 12 "Child age 12 (up to 15)" 21 "Child age 21 (down to 18)" 
 label values agez agez_vals 
 
 reshape long B1 N1 B2 N2, i(temp agez) j(parent) string
 drop temp 
 sort parent agez 
 
 label var B1 "Early Period IRPs"
 label var B2 "Late Period IRPs"
 
 list
 
 gen spec=1
 
qui compress 
save ${us_results}/US_irps_iv_v2.dta, replace


 
 
*************************************************
* SPECIFICATION 2: ONE PARENT, INCOME & HC
*************************************************
use `rankedsample', clear

************************
* MOTHERS 

  
* RESIDUALIZE average measure for child and mother for IRP with average parent income
 foreach var in pLAB pm_LAB m_schmax {
	qui regress `var' m_LABAGE_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE annual measures for mother -- use endogenous measure age
 foreach var in pm_LAB_12  pm_LAB_a16  pm_LAB_21 {
	qui regress `var' m_LABAGE16_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE child income for IV approach (add "1" to variable name) with mothers
 foreach var in pLAB m_schmax {
	qui regress `var' m_LABAGE16_? AGEC? i.year 
	predict `var'1_r, resid
 }

**** Early period   
preserve
local t=1
 loc yrmin=1985
 loc yrmax=1995
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'm H`t'm N`t'm

*IRP with parent average income
 regress pLAB_r pm_LAB_r m_schmax_r, cluster(newid)
 matrix B`t'm=_b[pm_LAB_r]
 matrix H`t'm=_b[m_schmax_r]
 matrix N`t'm=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pm_LAB_a16_r = pm_LAB_`a'_r ) m_schmax1_r, cluster(newid)
 matrix B`t'm=B`t'm,_b[pm_LAB_a16_r]
 matrix H`t'm=H`t'm,_b[m_schmax1_r]
 matrix N`t'm=N`t'm,e(N)
}
restore 

**** Late period 
preserve
local t=2
 loc yrmin=2008
 loc yrmax=2018
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'm H`t'm N`t'm

*IRP with parent average income
 regress pLAB_r pm_LAB_r m_schmax_r, cluster(newid)
 matrix B`t'm=_b[pm_LAB_r]
 matrix H`t'm=_b[m_schmax_r]
 matrix N`t'm=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pm_LAB_a16_r = pm_LAB_`a'_r ) m_schmax1_r, cluster(newid)
 matrix B`t'm=B`t'm,_b[pm_LAB_a16_r]
 matrix H`t'm=H`t'm,_b[m_schmax1_r]
 matrix N`t'm=N`t'm,e(N)
}
restore   

************************
* FATHERS 
  
* RESIDUALIZE average measure for child and father for IRP with average parent income
 drop pLAB_r
 foreach var in pLAB pf_LAB f_schmax {
	qui regress `var' f_LABAGE_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE annual measures for father -- use endogenous measure age
 foreach var in pf_LAB_12  pf_LAB_a16  pf_LAB_21 {
	qui regress `var' f_LABAGE16_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE child income for IV approach (add "1" to variable name) with fathers
 drop pLAB1_r
 foreach var in pLAB f_schmax {
	qui regress `var' f_LABAGE16_? AGEC? i.year 
	predict `var'1_r, resid
 }

**** Early period   
preserve
local t=1
 loc yrmin=1985
 loc yrmax=1995
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'f H`t'f N`t'f

*IRP with parent average income
 regress pLAB_r pf_LAB_r f_schmax_r, cluster(newid)
 matrix B`t'f=_b[pf_LAB_r]
 matrix H`t'f=_b[f_schmax_r]
 matrix N`t'f=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pf_LAB_a16_r = pf_LAB_`a'_r ) f_schmax1_r, cluster(newid)
 matrix B`t'f=B`t'f,_b[pf_LAB_a16_r]
 matrix H`t'f=H`t'f,_b[f_schmax1_r]
 matrix N`t'f=N`t'f,e(N)
}
restore 

**** Late period 
preserve
local t=2
 loc yrmin=2008
 loc yrmax=2018
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'f H`t'f N`t'f

*IRP with parent average income
 regress pLAB_r pf_LAB_r f_schmax_r, cluster(newid)
 matrix B`t'f=_b[pf_LAB_r]
 matrix H`t'f=_b[f_schmax_r]
 matrix N`t'f=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pf_LAB_a16_r = pf_LAB_`a'_r ) f_schmax1_r, cluster(newid)
 matrix B`t'f=B`t'f,_b[pf_LAB_a16_r]
 matrix H`t'f=H`t'f,_b[f_schmax1_r]
 matrix N`t'f=N`t'f,e(N)
}
restore  

**** Save results to datset 

clear 
set obs 1
gen temp=1 
forv t=1/2 {
 svmat B`t'm
 svmat B`t'f
 svmat H`t'm
 svmat H`t'f
 svmat N`t'm
 svmat N`t'f
 }
 
 reshape long B1m B2m H1m H2m N1m N2m B1f B2f H1f H2f N1f N2f, i(temp) j(agez) 
 replace agez=0  if agez==1
 replace agez=12 if agez==2
 replace agez=21 if agez==3
 
 label define agez_vals 0 "avg" 12 "Child age 12 (up to 15)" 21 "Child age 21 (down to 18)" 
 label values agez agez_vals 
  
 reshape long B1 H1 N1 B2 H2 N2, i(temp agez) j(parent) string
 drop temp 
 sort parent agez 
 
 label var B1 "Early Period IRPs"
 label var B2 "Late Period IRPs"
 label var H1 "Early Period coef on HC"
 label var H2 "Late Period coef on HC"
 
 list agez parent B1 H1 N1 B2 H2 N2

 gen spec=2 
 
qui compress 
append using ${us_results}/US_irps_iv_v2.dta 
save ${us_results}/US_irps_iv_v2.dta , replace


*************************************************
* SPECIFICATION 3: BOTH PARENTS, INCOME & HC 
*************************************************
use `rankedsample', clear

* RESIDUALIZE average measure for child and parent for IRP with average parent income
 foreach var in pLAB pm_LAB m_schmax pf_LAB f_schmax {
	qui regress `var' m_LABAGE_? f_LABAGE_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE annual measures for parents -- use endogenous measure age
 foreach var in pm_LAB_12 pm_LAB_a16  pm_LAB_21 pf_LAB_12  pf_LAB_a16  pf_LAB_21 {
	qui regress `var' m_LABAGE16_? f_LABAGE16_? AGEC? i.year 
	predict `var'_r, resid
 }

* RESDIUALIZE child income for IV approach (add "1" to variable name) with mothers
 foreach var in pLAB m_schmax f_schmax {
	qui regress `var' m_LABAGE16_? f_LABAGE16_? AGEC? i.year 
	predict `var'1_r, resid
 }

**** Early period   
preserve
local t=1
 loc yrmin=1985
 loc yrmax=1995
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'm H`t'm N`t'm B`t'f H`t'f

*IRP with parent average income
 regress pLAB_r pm_LAB_r m_schmax_r pf_LAB_r f_schmax_r , cluster(newid)
 matrix B`t'm=_b[pm_LAB_r]
 matrix H`t'm=_b[m_schmax_r]
 matrix B`t'f=_b[pf_LAB_r]
 matrix H`t'f=_b[f_schmax_r]
 matrix N`t'm=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pm_LAB_a16_r pf_LAB_a16_r = pm_LAB_`a'_r pf_LAB_`a'_r ) m_schmax1_r f_schmax1_r, cluster(newid)
 matrix B`t'm=B`t'm,_b[pm_LAB_a16_r]
 matrix H`t'm=H`t'm,_b[m_schmax1_r]
 matrix B`t'f=B`t'f,_b[pf_LAB_a16_r]
 matrix H`t'f=H`t'f,_b[f_schmax1_r]
 matrix N`t'm=N`t'm,e(N)
}
restore 

**** Late period 
preserve
local t=2
 loc yrmin=2008
 loc yrmax=2018
 keep if inrange(year,`yrmin',`yrmax') 
 
 capture noisily matrix drop B`t'm H`t'm N`t'm

*IRP with parent average income
 regress pLAB_r pm_LAB_r m_schmax_r pf_LAB_r f_schmax_r , cluster(newid)
 matrix B`t'm=_b[pm_LAB_r]
 matrix H`t'm=_b[m_schmax_r]
 matrix B`t'f=_b[pf_LAB_r]
 matrix H`t'f=_b[f_schmax_r]
 matrix N`t'm=e(N)
 
*IRPs using one parent income rank to instrument for another
foreach a in 12 21 {
 ivregress 2sls pLAB1_r (pm_LAB_a16_r pf_LAB_a16_r = pm_LAB_`a'_r pf_LAB_`a'_r ) m_schmax1_r f_schmax1_r, cluster(newid)
 matrix B`t'm=B`t'm,_b[pm_LAB_a16_r]
 matrix H`t'm=H`t'm,_b[m_schmax1_r]
 matrix B`t'f=B`t'f,_b[pf_LAB_a16_r]
 matrix H`t'f=H`t'f,_b[f_schmax1_r]
 matrix N`t'm=N`t'm,e(N)
}
restore  

 
 
 
 
 
 
 
 
 

**** Save results to datset 

clear 
set obs 1
gen temp=1 
forv t=1/2 {
 svmat B`t'm
 svmat B`t'f
 svmat H`t'm
 svmat H`t'f
 svmat N`t'm
 }
 
 reshape long B1m B2m H1m H2m N1m N2m B1f B2f H1f H2f N1f N2f, i(temp) j(agez) 
 replace agez=0  if agez==1
 replace agez=12 if agez==2
 replace agez=21 if agez==3
 
 label define agez_vals 0 "avg" 12 "Child age 12 (up to 15)" 21 "Child age 21 (down to 18)" 
 label values agez agez_vals 
 
 reshape long B1 H1 N1 B2 H2 N2, i(temp agez) j(parent) string
 drop temp 
 sort parent agez 
 
 label var B1 "Early Period IRPs"
 label var B2 "Late Period IRPs"
 label var H1 "Early Period coef on HC"
 label var H2 "Late Period coef on HC"
 
 list agez parent B1 H1 N1 B2 H2 N2
 
 gen spec=3
 
qui compress 
append using ${us_results}/US_irps_iv_v2.dta 
save ${us_results}/US_irps_iv_v2.dta , replace
clear
log close 






